package controller;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.BaseServlet;
import model.LoginDao;

/**
 * Servlet implementation class UserEdit
 */
public class UserEdit extends BaseServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public UserEdit() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		if(!isLanUser(request)|!isSuperUser(request)) {
			response.sendRedirect("login");
			return;
		}

		LoginDao lDao = new LoginDao();
		

		String username = request.getParameter("username");
		String todo = request.getParameter("todo");

		// System.out.println(username);
		RequestDispatcher r = null;

		if (todo.equals("edit")) {
			try {
				System.out.println("Editing user");
				// System.out.println(username);
				ResultSet set = lDao.selectUserbyName(username);

				set.next();
				System.out.println(set.getString("username"));
				System.out.println(set.getString("password"));

				request.setAttribute("editing", "true");

				request.setAttribute("username", set.getString("username"));
				request.setAttribute("password", set.getString("password"));
				r = request.getRequestDispatcher("WEB-INF/UserEdit.jsp");

			} catch (SQLException se) {
				se.printStackTrace();
			}

		} else if (todo.equals("delete")) {
			lDao.deleteUser(username);
			r = request.getRequestDispatcher("UserPage");
			// r.forward(request, response);
		} else if (todo.equals("new")) {
			r = request.getRequestDispatcher("WEB-INF/UserEdit.jsp");
		} else {
			response.sendRedirect("UserPage");
		}

		r.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {	
		if (!request.getParameter("usernameform").equals("") && !request.getParameter("passwordform").equals("")) {
			LoginDao lDao = new LoginDao();			
			if (request.getParameter("editing").equals("true")) {
				// edit user
				lDao.changePassword(request.getParameter("usernameform"),request.getParameter("passwordform"));			
			} else {
				// new user
				lDao.createUser(request.getParameter("usernameform"),request.getParameter("passwordform"));
			}
			RequestDispatcher r = request.getRequestDispatcher("UserPage");
			r.forward(request, response);
		} else {
			System.out.println("Lege username en zo");
			request.setAttribute("fail", true);
			RequestDispatcher r = request.getRequestDispatcher("WEB-INF/UserEdit.jsp");
			r.forward(request, response);
		}
		

	}

}
